################################################################################################
# This script takes the recoded data for each treatment and runs the following test:
# 
# - Did subjects change how the allocated votes after poll results? (SV/QV).
# - Number of points on abstentions (SV/QV).
# - Were votes casted on abstentions? (SV/QV).
# 
# - Did subjects vote on the issue that had the highest points? (SV).
# 
# - Is red vote on highest/lowest point? (QV).
# - Did subjects change vote class? (QV).
# - Proportion of vote classes (QV), 
# 
# Exports the "mutated" dataframes for QV and SV. Also exports the aggregate summary data
# for the tests mentioned above.
# 
# Author: Luis S.
# Last Modified: 7.23.16
################################################################################################

library(dplyr)
library(ggplot2)
library(stargazer)

# setwd("C:/Users/Luis/Dropbox/Research/Storable Votes vs. Quadratic Voting Experiment/Experiment Analysis & Results/")

####################################
####### SV Preliminary Tests #######
###################################%

SV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_SV_Stage_2_clean.csv", header = T, stringsAsFactors = F)

# Determine if bonus vote changed with poll information
SV_data <- mutate(SV_data, VoteChanged = ifelse(SV1_Vote != SV2_Vote, "Yes", "No"), 
                  SV1_VoteOnAbstain = NA, SV2_VoteOnAbstain = NA, TotalAbstentions = NA,
                  TotalAbstentionsWithPoints = NA, TotalPointsOnAbstentions = NA, 
                  SV1_VoteOnMaxPoints = "No", SV2_VoteOnMaxPoints = "No", 
                  PrefMax_Immigration = NA, PrefMax_TeacherTenure = NA, 
                  PrefMax_PublicBonds = NA, PrefMax_BilingualEduc = NA)

for(row in 1:nrow(SV_data))
{
  # Number of abstentions with points > 0 and total points allocated/wasted on these issues
  totalAbstentions <- 0
  pointsOnAbstentions <- 0
  abstentionsWithPoints <- 0
  subjectAbstained <- FALSE
  
  
  if(SV_data$Ref1_BilingualEduc[row] == "Abstain")
  {
    subjectAbstained <- TRUE
    totalAbstentions <- totalAbstentions + 1
    
    if(SV_data$EducationPref[row] > 0)
    {
      
      pointsOnAbstentions <- pointsOnAbstentions + SV_data$EducationPref[row]
      abstentionsWithPoints <- abstentionsWithPoints + 1
    }
  }
  
  if(SV_data$Ref2_TeacherTenure[row] == "Abstain")
  {
    subjectAbstained <- TRUE
    totalAbstentions <- totalAbstentions + 1
    
    if(SV_data$TeachersPref[row] > 0)
    {
      pointsOnAbstentions <- pointsOnAbstentions + SV_data$TeachersPref[row]
      abstentionsWithPoints <- abstentionsWithPoints + 1
    }
    
  }
  
  if(SV_data$Ref3_PublicBonds[row] == "Abstain")
  {
    subjectAbstained <- TRUE
    totalAbstentions <- totalAbstentions + 1
    
    if(SV_data$BondsPref[row] > 0)
    {
      pointsOnAbstentions <- pointsOnAbstentions + SV_data$BondsPref[row]
      abstentionsWithPoints <- abstentionsWithPoints + 1
    }
    
  }
  
  if(SV_data$Ref4_Immigration[row] == "Abstain")
  {
    subjectAbstained <- TRUE
    totalAbstentions <- totalAbstentions + 1
    
    if(SV_data$ImmigrationPref[row] > 0)
    {
      pointsOnAbstentions <- pointsOnAbstentions + SV_data$ImmigrationPref[row]
      abstentionsWithPoints <- abstentionsWithPoints + 1
    }
  }
  
  SV_data$TotalAbstentions[row] <- totalAbstentions
  
  # Only writes a value if subject abstained. Otherwise it keeps NA 
  if(subjectAbstained)
  {
    SV_data$TotalPointsOnAbstentions[row] <- pointsOnAbstentions
    SV_data$TotalAbstentionsWithPoints[row] <- abstentionsWithPoints
  } 
  

  # Check if votes were used on issues where subject abstained (SV1 and SV2) - Only if subject abstained. Otherwise it keeps NA 
  if(subjectAbstained)
  {
    if((SV_data$SV1_Vote[row] == "BilingualEduc") & (SV_data$Ref1_BilingualEduc[row] == "Abstain")){SV_data$SV1_VoteOnAbstain[row] <- "Yes"
    } else if((SV_data$SV1_Vote[row] == "TeacherTenure") & (SV_data$Ref2_TeacherTenure[row] == "Abstain")){SV_data$SV1_VoteOnAbstain[row] <- "Yes"
    } else if((SV_data$SV1_Vote[row] == "PublicBonds") & (SV_data$Ref3_PublicBonds[row] == "Abstain")){SV_data$SV1_VoteOnAbstain[row] <- "Yes"
    } else if((SV_data$SV1_Vote[row] == "Immigration") & (SV_data$Ref4_Immigration[row] == "Abstain")){SV_data$SV1_VoteOnAbstain[row] <- "Yes"
    } else {SV_data$SV1_VoteOnAbstain[row] <- "No"}
    
    if((SV_data$SV2_Vote[row] == "BilingualEduc") & (SV_data$Ref1_BilingualEduc[row] == "Abstain")){SV_data$SV2_VoteOnAbstain[row] <- "Yes"
    } else if((SV_data$SV2_Vote[row] == "TeacherTenure") & (SV_data$Ref2_TeacherTenure[row] == "Abstain")){SV_data$SV2_VoteOnAbstain[row] <- "Yes"
    } else if((SV_data$SV2_Vote[row] == "PublicBonds") & (SV_data$Ref3_PublicBonds[row] == "Abstain")){SV_data$SV2_VoteOnAbstain[row] <- "Yes"
    } else if((SV_data$SV2_Vote[row] == "Immigration") & (SV_data$Ref4_Immigration[row] == "Abstain")){SV_data$SV2_VoteOnAbstain[row] <- "Yes"
    } else {SV_data$SV2_VoteOnAbstain[row] <- "No"}
  } 
  
  # Check if bonus vote was used on issue with most points (SV1 & SV2)
  maxPointAllocated = max(SV_data$EducationPref[row], SV_data$TeachersPref[row], SV_data$BondsPref[row], SV_data$ImmigrationPref[row])
  
  if((SV_data$SV1_Vote[row] == "BilingualEduc") & (SV_data$EducationPref[row] == maxPointAllocated)){SV_data$SV1_VoteOnMaxPoints[row] <- "Yes"}
  if((SV_data$SV1_Vote[row] == "TeacherTenure") & (SV_data$TeachersPref[row] == maxPointAllocated)){SV_data$SV1_VoteOnMaxPoints[row] <- "Yes"}
  if((SV_data$SV1_Vote[row] == "PublicBonds") & (SV_data$BondsPref[row] == maxPointAllocated)){SV_data$SV1_VoteOnMaxPoints[row] <- "Yes"}
  if((SV_data$SV1_Vote[row] == "Immigration") & (SV_data$ImmigrationPref[row] == maxPointAllocated)){SV_data$SV1_VoteOnMaxPoints[row] <- "Yes"}
  
  if((SV_data$SV2_Vote[row] == "BilingualEduc") & (SV_data$EducationPref[row] == maxPointAllocated)){SV_data$SV2_VoteOnMaxPoints[row] <- "Yes"}
  if((SV_data$SV2_Vote[row] == "TeacherTenure") & (SV_data$TeachersPref[row] == maxPointAllocated)){SV_data$SV2_VoteOnMaxPoints[row] <- "Yes"}
  if((SV_data$SV2_Vote[row] == "PublicBonds") & (SV_data$BondsPref[row] == maxPointAllocated)){SV_data$SV2_VoteOnMaxPoints[row] <- "Yes"}
  if((SV_data$SV2_Vote[row] == "Immigration") & (SV_data$ImmigrationPref[row] == maxPointAllocated)){SV_data$SV2_VoteOnMaxPoints[row] <- "Yes"}
  
  # determine what issues received the maximum amount of points
  
  SV_data$PrefMax_Immigration[row] <- ifelse(SV_data$ImmigrationPref[row] == maxPointAllocated, T, F)
  SV_data$PrefMax_TeacherTenure[row] <- ifelse(SV_data$TeachersPref[row] == maxPointAllocated, T, F)
  SV_data$PrefMax_PublicBonds[row] <- ifelse(SV_data$BondsPref[row] == maxPointAllocated, T, F)
  SV_data$PrefMax_BilingualEduc[row] <- ifelse(SV_data$EducationPref[row] == maxPointAllocated, T, F)
}

# Replaces blanks with NA strings and exports dataset
SV_data[is.na(SV_data)] <- "NA"
write.csv(SV_data, "California Stage 2 - SV & QV Data/Summaries & Tests/California_MTurk_SV_Stage_2_withTests.csv", row.names = F)



####################################
####### QV Preliminary Tests #######
###################################%

QV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_QV_Stage_2_clean.csv", header = T, stringsAsFactors = F)
QV_data[is.na(QV_data)] <- 0

# Determine if bonus vote changed with poll information
QV_data <- mutate(QV_data, ClassChanged = ifelse(QV1_Class != QV2_Class, "Yes", "No"),VoteChanged = NA, 
                  QV1_VoteOnAbstain = NA, QV2_VoteOnAbstain = NA, TotalAbstentions = NA,
                  TotalAbstentionsWithPoints = NA, TotalPointsOnAbstentions = NA, 
                  QV1_RedVoteOnMaxMinPoints = NA, QV2_RedVoteOnMaxMinPoints = NA, 
                  PrefMax_Immigration = NA, PrefMax_TeacherTenure = NA, 
                  PrefMax_PublicBonds = NA, PrefMax_BilingualEduc = NA)

# Determines the index of columns for the votes
colsQV1 <- grepl("QV1_Vote_",colnames(QV_data))
colsQV2 <- grepl("QV2_Vote_",colnames(QV_data))

for(row in 1:nrow(QV_data))
{
  # Check if vote changed in QV1 and QV1 after poll was presented
  QV_data$VoteChanged[row] <- ifelse(all(QV_data[row, colsQV1] == QV_data[row,colsQV2]), "No", "Yes")
  
  # Number of abstentions with points > 0 and total points allocated/wasted on these issues
  totalAbstentions <- 0
  pointsOnAbstentions <- 0
  abstentionsWithPoints <- 0
  subjectAbstained <- FALSE
  
  
  if(QV_data$Ref1_BilingualEduc[row] == "Abstain")
  {
    subjectAbstained <- TRUE
    totalAbstentions <- totalAbstentions + 1
    
    if(QV_data$EducationPref[row] > 0)
    {
      
      pointsOnAbstentions <- pointsOnAbstentions + QV_data$EducationPref[row]
      abstentionsWithPoints <- abstentionsWithPoints + 1
    }
  }
  
  if(QV_data$Ref2_TeacherTenure[row] == "Abstain")
  {
    subjectAbstained <- TRUE
    totalAbstentions <- totalAbstentions + 1
    
    if(QV_data$TeachersPref[row] > 0)
    {
      pointsOnAbstentions <- pointsOnAbstentions + QV_data$TeachersPref[row]
      abstentionsWithPoints <- abstentionsWithPoints + 1
    }
    
  }
  
  if(QV_data$Ref3_PublicBonds[row] == "Abstain")
  {
    subjectAbstained <- TRUE
    totalAbstentions <- totalAbstentions + 1
    
    if(QV_data$BondsPref[row] > 0)
    {
      pointsOnAbstentions <- pointsOnAbstentions + QV_data$BondsPref[row]
      abstentionsWithPoints <- abstentionsWithPoints + 1
    }
    
  }
  
  if(QV_data$Ref4_Immigration[row] == "Abstain")
  {
    subjectAbstained <- TRUE
    totalAbstentions <- totalAbstentions + 1
    
    if(QV_data$ImmigrationPref[row] > 0)
    {
      pointsOnAbstentions <- pointsOnAbstentions + QV_data$ImmigrationPref[row]
      abstentionsWithPoints <- abstentionsWithPoints + 1
    }
  }
  
  QV_data$TotalAbstentions[row] <- totalAbstentions
  
  # Only writes a value if subject abstained. Otherwise it keeps NA 
  if(subjectAbstained)
  {
    QV_data$TotalPointsOnAbstentions[row] <- pointsOnAbstentions
    QV_data$TotalAbstentionsWithPoints[row] <- abstentionsWithPoints
  }
  
  # Check if votes were used on issues where subject abstained (QV1 and QV2) - Only if subject abstained. Otherwise it keeps NA 
  if(subjectAbstained)
  {
    if((QV_data$QV1_Vote_BilingualEduc[row] == 1) & (QV_data$Ref1_BilingualEduc[row] == "Abstain")){QV_data$QV1_VoteOnAbstain[row] <- "Yes"
    } else if((QV_data$QV1_Vote_TeacherTenure[row] == 1) & (QV_data$Ref2_TeacherTenure[row] == "Abstain")){QV_data$QV1_VoteOnAbstain[row] <- "Yes"
    } else if((QV_data$QV1_Vote_PublicBonds[row] == 1) & (QV_data$Ref3_PublicBonds[row] == "Abstain")){QV_data$QV1_VoteOnAbstain[row] <- "Yes"
    } else if((QV_data$QV1_Vote_Immigration[row] == 1) & (QV_data$Ref4_Immigration[row] == "Abstain")){QV_data$QV1_VoteOnAbstain[row] <- "Yes"
    } else (QV_data$QV1_VoteOnAbstain[row] <- "No")
    
    if((QV_data$QV2_Vote_BilingualEduc[row] == 1) & (QV_data$Ref1_BilingualEduc[row] == "Abstain")){QV_data$QV2_VoteOnAbstain[row] <- "Yes"
    } else if((QV_data$QV2_Vote_TeacherTenure[row] == 1) & (QV_data$Ref2_TeacherTenure[row] == "Abstain")){QV_data$QV2_VoteOnAbstain[row] <- "Yes"
    } else if((QV_data$QV2_Vote_PublicBonds[row] == 1) & (QV_data$Ref3_PublicBonds[row] == "Abstain")){QV_data$QV2_VoteOnAbstain[row] <- "Yes"
    } else if((QV_data$QV2_Vote_Immigration[row] == 1) & (QV_data$Ref4_Immigration[row] == "Abstain")){QV_data$QV2_VoteOnAbstain[row] <- "Yes"
    } else (QV_data$QV2_VoteOnAbstain[row] <- "No")
  }

  # Check if Red Vote was used on issue with most/least points (QV1 & QV2)
  if(QV_data$QV1_Class[row] == "(1) Red")
  {
    # Determine if it was allocated on maximum points
    maxPointAllocated = max(QV_data$EducationPref[row], QV_data$TeachersPref[row], QV_data$BondsPref[row], QV_data$ImmigrationPref[row])
    
    if((QV_data$QV1_Vote_BilingualEduc[row] == 1) & (QV_data$EducationPref[row] == maxPointAllocated)){QV_data$QV1_RedVoteOnMaxMinPoints[row] <- "Max"
    } else if((QV_data$QV1_Vote_TeacherTenure[row] == 1) & (QV_data$TeachersPref[row] == maxPointAllocated)){QV_data$QV1_RedVoteOnMaxMinPoints[row] <- "Max"
    } else if((QV_data$QV1_Vote_PublicBonds[row] == 1) & (QV_data$BondsPref[row] == maxPointAllocated)){QV_data$QV1_RedVoteOnMaxMinPoints[row] <- "Max"
    } else if((QV_data$QV1_Vote_Immigration[row] == 1) & (QV_data$ImmigrationPref[row] == maxPointAllocated)){QV_data$QV1_RedVoteOnMaxMinPoints[row] <- "Max"
    } else {
      
      # Determine if it was allocated on minimum points
      minPointAllocated = min(QV_data$EducationPref[row], QV_data$TeachersPref[row], QV_data$BondsPref[row], QV_data$ImmigrationPref[row])
      
      if((QV_data$QV1_Vote_BilingualEduc[row] == 1) & (QV_data$EducationPref[row] == maxPointAllocated)){QV_data$QV1_RedVoteOnMaxMinPoints[row] <- "Min"
      } else if((QV_data$QV1_Vote_TeacherTenure[row] == 1) & (QV_data$TeachersPref[row] == maxPointAllocated)){QV_data$QV1_RedVoteOnMaxMinPoints[row] <- "Min"
      } else if((QV_data$QV1_Vote_PublicBonds[row] == 1) & (QV_data$BondsPref[row] == maxPointAllocated)){QV_data$QV1_RedVoteOnMaxMinPoints[row] <- "Min"
      } else if((QV_data$QV1_Vote_Immigration[row] == 1) & (QV_data$ImmigrationPref[row] == maxPointAllocated)){QV_data$QV1_RedVoteOnMaxMinPoints[row] <- "Min"
      } else {QV_data$QV1_RedVoteOnMaxMinPoints[row] <- "Neither"}
    }
  }
  
  if(QV_data$QV2_Class[row] == "(1) Red")
  {
    # Determine if it was allocated on maximum points
    maxPointAllocated = max(QV_data$EducationPref[row], QV_data$TeachersPref[row], QV_data$BondsPref[row], QV_data$ImmigrationPref[row])
    
    if((QV_data$QV2_Vote_BilingualEduc[row] == 1) & (QV_data$EducationPref[row] == maxPointAllocated)){QV_data$QV2_RedVoteOnMaxMinPoints[row] <- "Max"
    } else if((QV_data$QV2_Vote_TeacherTenure[row] == 1) & (QV_data$TeachersPref[row] == maxPointAllocated)){QV_data$QV2_RedVoteOnMaxMinPoints[row] <- "Max"
    } else if((QV_data$QV2_Vote_PublicBonds[row] == 1) & (QV_data$BondsPref[row] == maxPointAllocated)){QV_data$QV2_RedVoteOnMaxMinPoints[row] <- "Max"
    } else if((QV_data$QV2_Vote_Immigration[row] == 1) & (QV_data$ImmigrationPref[row] == maxPointAllocated)){QV_data$QV2_RedVoteOnMaxMinPoints[row] <- "Max"
    } else {
      
      # Determine if it was allocated on minimum points
      minPointAllocated = min(QV_data$EducationPref[row], QV_data$TeachersPref[row], QV_data$BondsPref[row], QV_data$ImmigrationPref[row])
      
      if((QV_data$QV2_Vote_BilingualEduc[row] == 1) & (QV_data$EducationPref[row] == maxPointAllocated)){QV_data$QV2_RedVoteOnMaxMinPoints[row] <- "Min"
      } else if((QV_data$QV2_Vote_TeacherTenure[row] == 1) & (QV_data$TeachersPref[row] == maxPointAllocated)){QV_data$QV2_RedVoteOnMaxMinPoints[row] <- "Min"
      } else if((QV_data$QV2_Vote_PublicBonds[row] == 1) & (QV_data$BondsPref[row] == maxPointAllocated)){QV_data$QV2_RedVoteOnMaxMinPoints[row] <- "Min"
      } else if((QV_data$QV2_Vote_Immigration[row] == 1) & (QV_data$ImmigrationPref[row] == maxPointAllocated)){QV_data$QV2_RedVoteOnMaxMinPoints[row] <- "Min"
      } else {QV_data$QV2_RedVoteOnMaxMinPoints[row] <- "Neither"}
    }
  }

  maxPointAllocated <- max(QV_data$EducationPref[row], QV_data$TeachersPref[row], QV_data$BondsPref[row], QV_data$ImmigrationPref[row])
  
  QV_data$PrefMax_Immigration[row] <- ifelse(QV_data$ImmigrationPref[row] == maxPointAllocated, T, F)
  QV_data$PrefMax_TeacherTenure[row] <- ifelse(QV_data$TeachersPref[row] == maxPointAllocated, T, F)
  QV_data$PrefMax_PublicBonds[row] <- ifelse(QV_data$BondsPref[row] == maxPointAllocated, T, F)
  QV_data$PrefMax_BilingualEduc[row] <- ifelse(QV_data$EducationPref[row] == maxPointAllocated, T, F)
}

# Replaces blanks with NA strings and exports dataset
QV_data[is.na(QV_data)] <- "NA"
write.csv(QV_data, "California Stage 2 - SV & QV Data/Summaries & Tests/California_MTurk_QV_Stage_2_withTests.csv", row.names = F)


####################################
###### Exporting Summary Data ######
####################################

####### SV #######

test1 <- group_by(SV_data, TotalAbstentions, TotalAbstentionsWithPoints) %>% summarise(Subject_Count = n())
test2 <- group_by(SV_data, SV1_VoteOnAbstain, SV2_VoteOnAbstain) %>% summarise(VOA_Count = n())
test3 <- group_by(SV_data, SV1_VoteOnMaxPoints, SV2_VoteOnMaxPoints) %>% summarise(VOMP_Count = n())
test4 <- group_by(SV_data, VoteChanged) %>% summarise(VC_Count = n())


temp1 <- SV_data %>% group_by(PrefMax_BilingualEduc, Ref1_BilingualEduc) %>% summarize(Count = n()) %>% 
  filter(PrefMax_BilingualEduc == T) %>% rename(Position = Ref1_BilingualEduc) %>% ungroup() %>% select(Position, Count)
temp1 <- data.frame(Issue = "Bilingual Educ", Position = temp1$Position, Count = temp1$Count,  stringsAsFactors = F)
temp2 <- SV_data %>% group_by(PrefMax_TeacherTenure, Ref2_TeacherTenure) %>% summarize(Count = n()) %>% 
  filter(PrefMax_TeacherTenure == T) %>% rename(Position = Ref2_TeacherTenure) %>% ungroup() %>% select(Position, Count)
temp2 <- data.frame(Issue = "Teacher Tenure", Position = temp2$Position, Count = temp2$Count,  stringsAsFactors = F)
temp3 <- SV_data %>% group_by(PrefMax_PublicBonds, Ref3_PublicBonds) %>% summarize(Count = n()) %>% 
  filter(PrefMax_PublicBonds == T) %>% rename(Position = Ref3_PublicBonds) %>% ungroup() %>% select(Position, Count)
temp3 <- data.frame(Issue = "Public Bonds", Position = temp3$Position, Count = temp3$Count,  stringsAsFactors = F)
temp4 <- SV_data %>% group_by(PrefMax_Immigration, Ref4_Immigration) %>% summarize(Count = n()) %>% 
  filter(PrefMax_Immigration == T) %>% rename(Position = Ref4_Immigration) %>% ungroup() %>% select(Position, Count)
temp4 <- data.frame(Issue = "Immigration", Position = temp4$Position, Count = temp4$Count,  stringsAsFactors = F)
test5 <- bind_rows(temp1,temp2,temp3,temp4) %>% rename(IssueWithMaxPref = Issue) %>% group_by(IssueWithMaxPref) %>% mutate(Total = sum(Count))

# Print them out to the folder 
sink("California Stage 2 - SV & QV Data/Summaries & Tests/SV_Tables of Tests & Summaries.txt")
stargazer(test1, type = "text", summary = FALSE, rownames = FALSE)
stargazer(test2, type = "text", summary = FALSE, rownames = FALSE)
stargazer(test3, type = "text", summary = FALSE, rownames = FALSE)
stargazer(test4, type = "text", summary = FALSE, rownames = FALSE)
stargazer(test5, type = "text", summary = FALSE, rownames = FALSE)
sink()


####### QV #######

test1 <- group_by(QV_data, TotalAbstentions, TotalAbstentionsWithPoints) %>% summarise(Subject_Count = n())
test2 <- group_by(QV_data, QV1_VoteOnAbstain, QV2_VoteOnAbstain) %>% summarise(VOA_Count = n())
test3 <- group_by(QV_data, QV1_VoteOnAbstain, QV1_Class) %>% 
  summarise(Subject_Count = n()) %>% filter(QV1_VoteOnAbstain == "Yes")
test4 <- group_by(QV_data, QV2_VoteOnAbstain, QV2_Class) %>% 
  summarise(Subject_Count = n()) %>% filter(QV2_VoteOnAbstain == "Yes")
test5 <- group_by(QV_data, QV1_RedVoteOnMaxMinPoints, QV2_RedVoteOnMaxMinPoints) %>% summarise(RVOMMP_Count = n())
test6 <- group_by(QV_data, QV1_Class, QV2_Class) %>% summarise(Subject_Count = n())
test7 <- group_by(QV_data, ClassChanged, VoteChanged) %>% summarise(CCandVC_Count = n())

temp1 <- QV_data %>% group_by(PrefMax_BilingualEduc, Ref1_BilingualEduc) %>% summarize(Count = n()) %>% 
  filter(PrefMax_BilingualEduc == T) %>% rename(Position = Ref1_BilingualEduc) %>% ungroup() %>% select(Position, Count)
temp1 <- data.frame(Issue = "Bilingual Educ", Position = temp1$Position, Count = temp1$Count,  stringsAsFactors = F)
temp2 <- QV_data %>% group_by(PrefMax_TeacherTenure, Ref2_TeacherTenure) %>% summarize(Count = n()) %>% 
  filter(PrefMax_TeacherTenure == T) %>% rename(Position = Ref2_TeacherTenure) %>% ungroup() %>% select(Position, Count)
temp2 <- data.frame(Issue = "Teacher Tenure", Position = temp2$Position, Count = temp2$Count,  stringsAsFactors = F)
temp3 <- QV_data %>% group_by(PrefMax_PublicBonds, Ref3_PublicBonds) %>% summarize(Count = n()) %>% 
  filter(PrefMax_PublicBonds == T) %>% rename(Position = Ref3_PublicBonds) %>% ungroup() %>% select(Position, Count)
temp3 <- data.frame(Issue = "Public Bonds", Position = temp3$Position, Count = temp3$Count,  stringsAsFactors = F)
temp4 <- QV_data %>% group_by(PrefMax_Immigration, Ref4_Immigration) %>% summarize(Count = n()) %>% 
  filter(PrefMax_Immigration == T) %>% rename(Position = Ref4_Immigration) %>% ungroup() %>% select(Position, Count)
temp4 <- data.frame(Issue = "Immigration", Position = temp4$Position, Count = temp4$Count,  stringsAsFactors = F)
test8 <- bind_rows(temp1,temp2,temp3,temp4) %>% rename(IssueWithMaxPref = Issue) %>% group_by(IssueWithMaxPref) %>% mutate(Total = sum(Count))

# Print them out to the folder 
sink("California Stage 2 - SV & QV Data/Summaries & Tests/QV_Tables of Tests & Summaries.txt")
stargazer(test1, type = "text", summary = FALSE, rownames = FALSE)
stargazer(test2, type = "text", summary = FALSE, rownames = FALSE)
stargazer(test3, type = "text", summary = FALSE, rownames = FALSE)
stargazer(test4, type = "text", summary = FALSE, rownames = FALSE)
stargazer(test5, type = "text", summary = FALSE, rownames = FALSE)
stargazer(test6, type = "text", summary = FALSE, rownames = FALSE)
stargazer(test7, type = "text", summary = FALSE, rownames = FALSE)
stargazer(test8, type = "text", summary = FALSE, rownames = FALSE)
sink()



##### QV - Histogram of Vote Classes ######

test1 <- group_by(QV_data, QV1_Class) %>% summarize(Count = n()) %>% 
  rename(Class = QV1_Class) %>% mutate(Percent = round(Count/sum(Count),digits = 3)*100, Stage = "QV1")
test2 <- group_by(QV_data, QV2_Class) %>% summarize(Count = n()) %>% 
  rename(Class = QV2_Class) %>% mutate(Percent = round(Count/sum(Count),digits = 3)*100, Stage = "QV2")
test3 <- bind_rows(test1, test2)

VCPlot <- ggplot(data = test3, aes(x = Class, y = Percent, fill=Stage)) + 
  geom_bar(stat="identity", position = "dodge") + scale_y_continuous(limits = c(0,100), breaks = seq(0, 100, by = 10)) +  
  xlab("Vote Class") + ylab("Percent of Subjects in Vote Class") + 
  theme(axis.text.x=element_text(size=12, vjust = -.05), legend.text=element_text(size=16), title=element_text(size=14)) +
  ggtitle("QV - Breakdown of Vote Classes Chosen by Subjects")
print(VCPlot)
ggsave(VCPlot, file="California Stage 2 - Experiment Plots/QV - Breakdown of Vote Classes Chosen by Subjects.png", height = 4, width = 7.5)
